package yf.mall.service.impl;

import com.yf.response.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;
import yf.mall.common.Const;
import yf.mall.dao.PointSignDao;
import yf.mall.dao.UserDao;
import yf.mall.entity.PointSignEntity;
import yf.mall.entity.SignEntity;
import yf.mall.entity.UserEntity;
import yf.mall.enums.AccountTypeEnum;
import yf.mall.service.AccountService;
import yf.mall.service.PointSignService;
import com.yf.service.impl.BaseServiceImpl;

/**
 * 积分签到Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Tue Nov 12 16:21:48 CST 2019
 */
@Service
@EnableScheduling
public class PointSignServiceImpl extends BaseServiceImpl<PointSignEntity> implements PointSignService {

    private PointSignDao pointSignDao;


    @Autowired
    public void setDao(PointSignDao pointSignDao) {
        this.pointSignDao = pointSignDao;
        super.setBaseDao(pointSignDao);
    }


    /**
     *
     *   @Override
     *     public List<UserVo> queryListVo(UserVo userVo) {
     *         return userDao.queryListVo(userVo);
     *     }
     *
     *     @Override
     *     public UserVo getVo(Long id) {
     *         return userDao.getVo(id);
     *     }

     @Override List<PointSignVo> queryListVo(PointSignVo pointSignVo){
     return pointSignDao.queryListVo(pointSignVo);
     }

     @Override PointSignVo getVo(Long id){
     return pointSignDao.getVo(id);
     }
     **/


    /**内部方法**/


    /**
     * 外部方法实现
     **/

    @Override
   @Transactional(rollbackFor = Exception.class)
    public PointSignEntity sign(Long userId, BigDecimal amount) {
        PointSignEntity signEntity = pointSignDao.findByUserId(userId);
        UserEntity userEntity = userDao.get(userId);
        //没签到过  签到一次
        if (signEntity == null) {
            signEntity = new PointSignEntity(userId, userEntity.getCode(), userEntity.getName(), 1, 1, new Date());
            pointSignDao.save(signEntity);
        } else {
            LocalDate signDate = signEntity.getTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDate nowDate = LocalDate.now();
            if (signDate.compareTo(nowDate) == 0) {
                throw new RRException("今日已经签到过!");
            }
            //如果昨天签到过
            if (nowDate.plusDays(-1).isEqual(signDate)) {
                signEntity.setCount(signEntity.getCount() + 1);
            } else {//重置
                signEntity.setCount(1);
            }
            signEntity.setSumCount(signEntity.getSumCount() + 1);
            //七天成倍奖励
            if (signEntity.getSumCount() == 7) {
                signEntity.setCount(1);
                amount = amount.multiply(new BigDecimal(7));
            }
            signEntity.setTime(new Date());
            pointSignDao.update(signEntity);
        }
        //奖励积分
        accountService.transfer(Const.ADMIN_ID, userId, AccountTypeEnum.POINT.getIndex(), amount, "签到积分奖励");

        return signEntity;
    }


    //每天凌晨一分钟后执行一次清空
    @Scheduled(cron = "0 1 0 * * ?")
   @Transactional(rollbackFor = Exception.class)
    public void initSign() {
        pointSignDao.initSign();
    }


    @Autowired
    private AccountService accountService;

    @Autowired
    private UserDao userDao;
}
